/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.sensor.cropcircle;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import nl.cloudfarming.client.model.SensorData;
import nl.cloudfarming.client.model.SensorDataFile;
import nl.cloudfarming.client.model.SensorMetadata;
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

/**
 *
 * @author Merijn Zengers
 */
public class CropCircleServiceImplIT {

    private CropcircleServiceImpl service;
    private static SensorMetadata METADATA;

    @Before
    public void setUp(){
         cleantable();
         service = new CropcircleServiceImpl();
         METADATA = new SensorMetadata("DDMMYY", 250, 2, "234,236", "test", "670,730,780");
         service.newSensorMetaData(METADATA);
    }

    @After
    public void cleantable(){
        service = new CropcircleServiceImpl();
        for(CropcircleSensorData sensorData : service.findAllCropcircleSensorData()){
            service.delete(sensorData);
        }
        for(SensorDataFile dataFile : service.findAllSensorDataFiles()){
            service.removeSensorDataFile(dataFile);
        }
        for(SensorMetadata metadata : service.findAllSensorMetadata()){
            service.remove(metadata);
        }
    }

    @Test
    public void createMetadata(){
        //
        int initialSize = service.findAllSensorMetadata().size();

        //Test
        SensorMetadata metadata = new SensorMetadata("DDMMYY", 250, 2, "234,236", "test", "670,730,780");
        service.newSensorMetaData(metadata);

        //Validate
        assertEquals(initialSize+1,service.findAllSensorMetadata().size());

        SensorMetadata dbMetadata = service.findMetadataById(metadata.getId());

        assertEquals(metadata,dbMetadata);
    }

    @Test
    public void createDataFile(){
        //Set up
        service.newSensorMetaData(METADATA);
        assertEquals(0,service.findAllSensorDataFiles().size());

        //Test
        SensorDataFile cDataFile = new SensorDataFile("Filename", "checkSum", new Date(), new Date(), METADATA);
        service.newSensorDataFile(cDataFile);

        //Validate
        assertEquals(1,service.findAllSensorDataFiles().size());

        SensorDataFile dbDatafile = service.findDataFileById(cDataFile.getId());

        assertEquals(cDataFile,dbDatafile);
    }

    @Test
    public void createSensorData(){
        //Set up
        service.newSensorMetaData(METADATA);
        
        SensorDataFile cDataFile = new SensorDataFile("Filename", "checkSum", new Date(), new Date(), METADATA);
        service.newSensorDataFile(cDataFile);
        
        assertEquals(0,service.findAllCropcircleSensorData().size());
        //Test
        CropcircleSensorData sensorData = new CropcircleSensorData();
        sensorData.setDataSensorFile(cDataFile);
        sensorData.setLongitude(123l);
        sensorData.setLatitude(456);
        sensorData.setSpeed(23.0);
        sensorData.setUtcTime( new Date());
        sensorData.setCourse(234.0);
        sensorData.setSensor(2356);
        sensorData.setVi1(3465);
        sensorData.setVi2(753);
        sensorData.setR1(123);
        sensorData.setR2(653);
        sensorData.setR3(135);
        sensorData.setAppRate("");

        List<CropcircleSensorData> sensorDataList = new ArrayList<CropcircleSensorData>();
        sensorDataList.add(sensorData);

        service.bulkPersist(sensorDataList,1);

        //Validate
        assertEquals(1,service.findAllCropcircleSensorData().size());

        SensorData dbSensorData = service.findSensorDataById(sensorData.getId());

        assertEquals(sensorData,dbSensorData);
    }
    //Tests if the not persisted sensordata and metadata is persisted when a datafile is persisted
    @Test
    public void datafilePersistTransitiveTest(){
        //Set up

        assertEquals(0,service.findAllCropcircleSensorData().size());
        assertEquals(0,service.findAllSensorDataFiles().size());

        SensorDataFile cDataFile = new SensorDataFile("Filename", "checkSum", new Date(), new Date(), METADATA);

        CropcircleSensorData sensorData = new CropcircleSensorData();
        sensorData.setDataSensorFile(cDataFile);
        sensorData.setLongitude(123l);
        sensorData.setLatitude(456);
        sensorData.setSpeed(23.0);
        sensorData.setUtcTime( new Date());
        sensorData.setCourse(234.0);
        sensorData.setSensor(2356);
        sensorData.setVi1(3465);
        sensorData.setVi2(753);
        sensorData.setR1(123);
        sensorData.setR2(653);
        sensorData.setR3(135);
        sensorData.setAppRate("");

        cDataFile.getSensorData().add(sensorData);

        //Test
        service.newSensorDataFile(cDataFile);

        //Validate

        assertEquals(METADATA,service.findMetadataById(METADATA.getId()));
        assertEquals(cDataFile,service.findDataFileById(cDataFile.getId()));
        assertTrue(sensorData.getId() != 0);
        assertEquals(sensorData,service.findSensorDataById(sensorData.getId()));
        assertEquals(1,service.findAllCropcircleSensorData().size());
        assertEquals(1,service.findAllSensorMetadata().size());
        assertEquals(1,service.findAllSensorDataFiles().size());
        
    }

    @Test 
    public void bulkPersistTest(){
        //setUp
        CropcircleServiceImpl service = new CropcircleServiceImpl();
        assertEquals(0,service.findAllCropcircleSensorData().size());
        assertEquals(0,service.findAllSensorDataFiles().size());

        SensorMetadata metadata = new SensorMetadata("DDMMYY", 350, 2, "234,236", "test", "670,730,780");
        SensorDataFile cDataFile = new SensorDataFile("Filename", "checkSum", new Date(), new Date(), metadata);

        SensorDataFile nonPersistedCDataFile = new SensorDataFile("Filename22", "checkSum22", new Date(), new Date(), metadata);

        service.newSensorDataFile(cDataFile);

        ArrayList<CropcircleSensorData> sensorDataList = new ArrayList<CropcircleSensorData>();
        CropcircleSensorData sensorData = new CropcircleSensorData();
        sensorData.setDataSensorFile(cDataFile);
        sensorData.setLongitude(123l);
        sensorData.setLatitude(456);
        sensorData.setSpeed(23.0);
        sensorData.setUtcTime( new Date());
        sensorData.setCourse(234.0);
        sensorData.setSensor(2356);
        sensorData.setVi1(3465);
        sensorData.setVi2(753);
        sensorData.setR1(123);
        sensorData.setR2(653);
        sensorData.setR3(135);
        sensorData.setAppRate("");
        sensorDataList.add(sensorData);

        CropcircleSensorData badSensorData = new CropcircleSensorData();
        badSensorData.setDataSensorFile(nonPersistedCDataFile);
        badSensorData.setLongitude(123l);
        badSensorData.setLatitude(456);
        badSensorData.setSpeed(23.0);
        badSensorData.setUtcTime( new Date());
        badSensorData.setCourse(234.0);
        badSensorData.setSensor(2356);
        badSensorData.setVi1(3465);
        badSensorData.setVi2(753);
        badSensorData.setR1(123);
        badSensorData.setR2(653);
        badSensorData.setR3(135);
        badSensorData.setAppRate("");
        sensorDataList.add(badSensorData);

        CropcircleSensorData sensorData2 = new CropcircleSensorData();
        sensorData2.setDataSensorFile(cDataFile);
        sensorData2.setLongitude(123l);
        sensorData2.setLatitude(456);
        sensorData2.setSpeed(23.0);
        sensorData2.setUtcTime( new Date());
        sensorData2.setCourse(234.0);
        sensorData2.setSensor(2356);
        sensorData2.setVi1(3465);
        sensorData2.setVi2(753);
        sensorData2.setR1(123);
        sensorData2.setR2(653);
        sensorData2.setR3(135);
        sensorData2.setAppRate("");
        sensorDataList.add(sensorData2);

        //Test
        service.bulkPersist(sensorDataList, 10);

        //Validate
        assertEquals(2,service.findAllCropcircleSensorData().size());
    }

    
}
